package cn.jiyun.mongodb;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

/**
 * @author Fay
 * @date 2022/6/29&16:41
 */

/**
 * 查询所有
 */
public class mongodbDemo {
    public static void main(String[] args) {
//        建立连接
        MongoClient client = new MongoClient("127.0.0.1");
        //获取数据库
        MongoDatabase spitdb = client.getDatabase("spitdb");
        //获取集合
        MongoCollection<Document> spit = spitdb.getCollection("spit");
        //获取数据
        FindIterable<Document> documents = spit.find();
        for (Document document : documents) {
            System.out.println("内容："+	document.getString("content"));
            System.out.println("用户ID:"+document.getString("userid"));
            System.out.println("浏览量："+document.getInteger("visits"));
            System.out.println("=========================");
        }
        client.close();
    }
}
